#include<stdio.h>
#include <string.h>
struct item
{
int ind;
char un[80],pwd[20];
}e;

enter()
{
FILE *f;
char ch;
int nl=0,i=1,d;
printf("\nEnter username(spaces not allowed):");
scanf("%s",e.un);
printf(" Enter Desired Password");
scanf("%s",e.pwd);



f=fopen("data.txt","a+");
if(f==NULL)
printf("cannot open file");

e.ind=hash(e.un);


fprintf(f,"%d",e.ind);
fputs("\t",f);
fputs(e.un,f);
fputs("\t",f);
fputs(e.pwd,f);
fputs("\n",f);

fclose(f);



}

int collision(int x)
{
FILE *t;
t=fopen("data.txt","a+");
//incomplete
}




int brk(char u[], int i)
{
int j;
switch(u[i])
{
case 'a':
case 'A': j=1;
break;
case 'b':
case 'B': j=2;
break;
case 'c':
case 'C': j=3;
break;
case 'd':
case 'D': j=4;
break;
case 'e':
case 'E': j=5;
break;
case 'f':
case 'F': j=6;
break;
case 'g':
case 'G': j=7;
break;
case 'h':
case 'H': j=8;
break;
case 'i':
case 'I': j=9;
break;
case 'j':
case 'J': j=10;
break;
case 'k':
case 'K': j=11;
break;
case 'l':
case 'L': j=12;
break;
case 'm':
case 'M': j=13;
break;
case 'n':
case 'N': j=14;
break;
case 'o':
case 'O': j=15;
break;
case 'p':
case 'P': j=16;
break;
case 'q':
case 'Q': j=17;
break;
case 'r':
case 'R': j=18;
break;
case 's':
case 'S': j=19;
break;
case 't':
case 'T': j=20;
break;
case 'u':
case 'U': j=21;
break;
case 'v':
case 'V': j=22;
break;
case 'w':
case 'W': j=23;
break;
case 'x':
case 'X': j=24;
break;
case 'y':
case 'Y': j=25;
break;
case 'z':
case 'Z': j=26;
break;
case '_':
case '.': j=27;
break;
default : j=0;
break;

}
return j;
}





int hash(char u[])
{
int len,i=0,k,sum=0,x,y;
len=strlen(u);
while(i<len)
{
k=brk(u,i);
sum+=k;
i++;
}
x=sum%20;
y=collision(x);
return y; 

}

check()
{
FILE *f;
char uname[80],pwd[20],ch;
int d,ctr=0,chn=0;
f=fopen("data.txt","r+");
printf("Enter username");
scanf("%s",uname);

d=hash(uname);
while ( fscanf ( f, "%d %s %s", &e.ind, e.un, e.pwd ) != EOF )
{
if ((e.ind==d)||(chn<3))
{
printf("Enter password:\t");
scanf("%s",pwd);
	if (strcmp(e.pwd,pwd)==0)
	{
	printf("Login Successful!!");
	}	
	else 
	{
	printf("Wrong Password or username!!!!");
	chn++;
	}

}

}
fclose(f);

}


void opt()
{
int opt1,opt2,opt3;
printf("1.New User \n 2.Existing user");
scanf("%d",&opt1);
if(opt1==1)
enter();
if(opt1==2)
check();
printf("Press 1 to continue");
scanf("%d",&opt3);
if (opt3==1)
opt();


}

void main()
{

opt();

}




